//
//  YWMaterialListCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/8/29.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWMaterialListCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "UIImageView+WebCache.h"
#import "YWmaterial.h"
#import "YWCommonTool.h"
#import "YWMaterialApply.h"
#import "YWMaterialApplyDetail.h"
#import "YWMaterialStockOut.h"
#import "YWMaterialStockOutDetail.h"
#import "YWMaterialReturn.h"
#import "YWMaterialReturnDetail.h"
#import "YWMaterialStock.h"
#import "YWMaterial.h"
#import "YWHandler.h"
#import "YYLabel.h"

@interface YWMaterialListCell()
/** 头像 */
@property (nonatomic, strong) UIImageView *headImageView;
/** 申请人名称 */
@property (nonatomic, strong) YYLabel *applyNameLabel;
/**  申请时间 */
@property (nonatomic, strong) YYLabel *applyDateLabel;
/** 物料状态 */
@property (nonatomic, strong) YYLabel *MaterialStateTypeLabel;
/**  申请单号 */
@property (nonatomic, strong) YYLabel *applyNumbersLabel;
/**  接收人 */
@property (nonatomic, strong) YYLabel *receivedManNameLabel;
/** 申请物料 */
@property (nonatomic, strong) YYLabel *materialsLabel;
@end

@implementation YWMaterialListCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        //self.separatorInset = UIEdgeInsetsMake(0, SCREEN_WIDTH, 0, 0);
    }
    return self;
}

- (void)setUpSubviews{
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.mas_equalTo(self.contentView).offset(8);
        make.width.and.height.mas_equalTo(@46);
    }];
    
    [self.applyNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_headImageView.mas_centerY).offset(-2);
        make.left.mas_equalTo(_headImageView.mas_right).offset(6);
    }];
    
    [self.applyDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_applyNameLabel);
        make.top.mas_equalTo(_applyNameLabel.mas_bottom).offset(4);
    }];
    
    [self.MaterialStateTypeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.baseline.mas_equalTo(_applyNameLabel);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    
    [self.applyNumbersLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_headImageView.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    
    [self.receivedManNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_applyNumbersLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
    }];
    
    [self.materialsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_receivedManNameLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
        make.bottom.mas_equalTo(self.contentView).offset(-6);
    }];
    
}

//- (void)layoutSubviews{
//    [super layoutSubviews];
//    
//}


- (void)setApply:(YWMaterialApplyViewModel *)apply{
    _apply = apply;
    //    头像
    NSString* imageUrl = [YWCommonTool pictureDownloadUrl:_apply.creator.headIcon];
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"profile_headimage"]];

    _applyNameLabel.text = _apply.creator.userName;
    _applyDateLabel.text = [YWCommonTool showTimeWithNormalFormatter:_apply.applyTime /1000];
    
    if (_apply.status == YWMaterialDataStatusCancel) {
        _MaterialStateTypeLabel.text = @"已撤回";
        _MaterialStateTypeLabel.textColor = [UIColor blackColor];
    }else{
        if (_apply.applyStatus == YWMaterialStatusNoReceive) {
            _MaterialStateTypeLabel.text = @"未领用";
            _MaterialStateTypeLabel.textColor = [UIColor blackColor];
        }else if (_apply.applyStatus == YWMaterialStatusHaveStockout){
            _MaterialStateTypeLabel.text = @"已领用";
            _MaterialStateTypeLabel.textColor = [UIColor blackColor];
        }else if (_apply.applyStatus == YWMaterialStatusHaveReturned){
            _MaterialStateTypeLabel.text = @"已归还";
            _MaterialStateTypeLabel.textColor = [UIColor blackColor];
        }else if (_apply.applyStatus == YWMaterialStatusReject){
            _MaterialStateTypeLabel.text = @"已驳回";
            _MaterialStateTypeLabel.textColor = [UIColor blackColor];
        }else if (_apply.applyStatus == YWMaterialStatusStockOutWaitCerturn){
            _MaterialStateTypeLabel.text = @"已出库待确认";
            _MaterialStateTypeLabel.textColor = [UIColor customRedColor];
        }else{
            _MaterialStateTypeLabel.text = @"已归还待确认";
            _MaterialStateTypeLabel.textColor = [UIColor customRedColor];
        }
    }
    _applyNumbersLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"申请单号: " firstColor:[UIColor customTextGrayBlackColor] secondText:_apply.applySequence secondColor:[UIColor customYellowColor] font:kContentFont];
    NSMutableString * receipientString = [NSMutableString string];
    [_apply.receivers enumerateObjectsUsingBlock:^(YWHandler *  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx!=0) {
            [receipientString appendString:@","];
        }
        [receipientString appendString:[NSString stringWithFormat:@"%@",handler.userName]];
    }];

    _receivedManNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"接收人: " firstColor:[UIColor customTextGrayBlackColor] secondText:receipientString secondColor:[UIColor customYellowColor] font:kContentFont];
    //物料
    NSMutableString * materialName = [NSMutableString string];
    [_apply.details enumerateObjectsUsingBlock:^(YWMaterialApplyDetail*  _Nonnull applyDetail, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx!=0) {
            [materialName appendString:@","];
        }
        [materialName appendString:[NSString stringWithFormat:@"%@",applyDetail.materialStock.materialInfo.materialName]];
    }];
    
    _materialsLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"物料: " firstColor:[UIColor customTextGrayBlackColor] secondText:materialName secondColor:[UIColor blackColor] font:kContentFont];
    
    self.receivedManNameLabel.preferredMaxLayoutWidth = self.receivedManNameLabel.frame.size.width;
    self.materialsLabel.preferredMaxLayoutWidth = self.materialsLabel.frame.size.width;
}

- (void)setRecipient:(YWMaterialStockOut *)recipient
{
    _recipient = recipient;
    NSString* imageUrl = [YWCommonTool pictureDownloadUrl:_recipient.receiver.headIcon];
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"profile_headimage"]];
    //领用单有，已领用，已归还两种状态
    if (_recipient.stockOutStatus == YWMaterialStatusHaveStockout) {
        _MaterialStateTypeLabel.text = @"已领用";
        _MaterialStateTypeLabel.textColor = [UIColor blackColor];
    }else if (_recipient.stockOutStatus == YWMaterialStatusReturnWaitCerturn){
        _MaterialStateTypeLabel.text = @"已归还待确认";
        _MaterialStateTypeLabel.textColor = [UIColor customRedColor];
    }else{
        _MaterialStateTypeLabel.text = @"已归还";
        _MaterialStateTypeLabel.textColor = [UIColor blackColor];
    }
    _applyNameLabel.text = _recipient.receiver.userName?:@"无";
    _applyDateLabel.text = [YWCommonTool showTimeWithNormalFormatter:_recipient.stockOutTime /1000];
    _applyNumbersLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"领用单号: " firstColor:[UIColor customTextGrayBlackColor] secondText:_recipient.stockOutSequence secondColor:[UIColor customYellowColor] font:kContentFont];
    NSString*userName = _recipient.creator.userName?:@"未知用户";
    _receivedManNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"发放人: " firstColor:[UIColor customTextGrayBlackColor] secondText:userName secondColor:[UIColor customYellowColor] font:kContentFont];
    //物料
    NSMutableString * materialName = [NSMutableString string];
    [_recipient.details enumerateObjectsUsingBlock:^(YWMaterialStockOutDetail*  _Nonnull stockOutDetail, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx!=0) {
            
            [materialName appendString:@","];
        }
        [materialName appendString:[NSString stringWithFormat:@"%@",stockOutDetail.materialStock.materialInfo.materialName]];
    }];
    _materialsLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"物料: " firstColor:[UIColor customTextGrayBlackColor] secondText:materialName.length?materialName:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    
    self.receivedManNameLabel.preferredMaxLayoutWidth = self.receivedManNameLabel.frame.size.width;
    self.materialsLabel.preferredMaxLayoutWidth = self.materialsLabel.frame.size.width;
}

- (void)setReturnBack:(YWMaterialReturn *)returnBack
{
    _returnBack = returnBack;
    NSString* imageUrl = [YWCommonTool pictureDownloadUrl:_returnBack.returner.headIcon];
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"profile_headimage"]];
    _applyNameLabel.text = _returnBack.returner.userName?:@"未知用户";
    _applyDateLabel.text = [YWCommonTool showTimeWithNormalFormatter:_returnBack.returnTime /1000];
    //归还单只有一个状态
    _MaterialStateTypeLabel.text = @"已归还";
    _applyNumbersLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"归还单号: " firstColor:[UIColor customTextGrayBlackColor] secondText:_returnBack.returnSequence?:@"无" secondColor:[UIColor customYellowColor] font:kContentFont];;
    _receivedManNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"接收人: " firstColor:[UIColor customTextGrayBlackColor] secondText:_returnBack.creator.userName?:@"无" secondColor:[UIColor customYellowColor] font:kContentFont];
    //物料
    NSMutableString * materialName = [NSMutableString string];
    [_returnBack.details enumerateObjectsUsingBlock:^(YWMaterialReturnDetail *  _Nonnull returnDetail, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx!=0) {
            [materialName appendString:@","];
        }
        [materialName appendString:[NSString stringWithFormat:@"%@",returnDetail.materialStock.materialInfo.materialName]];
        
    }];
    _materialsLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"物料: " firstColor:[UIColor customTextGrayBlackColor] secondText:materialName.length?materialName:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    
    self.receivedManNameLabel.preferredMaxLayoutWidth = self.receivedManNameLabel.frame.size.width;
    self.materialsLabel.preferredMaxLayoutWidth = self.materialsLabel.frame.size.width;
}

-(NSString *)getRecivedManString:(NSArray *)targeArray{
    NSString *handlers = [YWCommonTool handlerNamesWithHandlers:targeArray];
    return handlers;
}

- (UIImageView *)headImageView{
    if (!_headImageView) {
        UIImageView *imageView = [UIImageView new];
        imageView.clipsToBounds = YES;
        imageView.layer.cornerRadius = 46/2;
        _headImageView = imageView;
        [self.contentView addSubview:_headImageView];
    }
    return _headImageView;
}

- (YYLabel *)applyNameLabel{
    if (!_applyNameLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor blackColor];
        _applyNameLabel = label;
        [self.contentView addSubview:_applyNameLabel];
    }
    return _applyNameLabel;
}

- (YYLabel *)applyDateLabel{
    if (!_applyDateLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        _applyDateLabel = label;
        [self.contentView addSubview:_applyDateLabel];
    }
    return _applyDateLabel;
}

- (YYLabel *)MaterialStateTypeLabel{
    if (!_MaterialStateTypeLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor blackColor];
        _MaterialStateTypeLabel = label;
        [self.contentView addSubview:_MaterialStateTypeLabel];
    }
    return _MaterialStateTypeLabel;
}

- (YYLabel *)applyNumbersLabel{
    if (!_applyNumbersLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        _applyNumbersLabel = label;
        [self.contentView addSubview:_applyNumbersLabel];
    }
    return _applyNumbersLabel;
}

- (YYLabel *)receivedManNameLabel{
    if (!_receivedManNameLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _receivedManNameLabel = label;
        [self.contentView addSubview:_receivedManNameLabel];
    }
    return _receivedManNameLabel;
}

- (YYLabel *)materialsLabel{
    if (!_materialsLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _materialsLabel = label;
        [self.contentView addSubview:_materialsLabel];
    }
    return _materialsLabel;
}

@end
